<?php
// 连接数据库
class Db {
  static public function getDB() {
    try {

      $pdo = new PDO('mysql:host=localhost;dbname=test', 'root', 'password');  
      $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);  
      $pdo->exec('set names utf8'); 
/*添加*/
//$sql = "INSERT INTO `user` SET `login`=:login AND `password`=:password"; 
// $sql = "INSERT INTO `user` (`login` ,`password`)VALUES (:login, :password)";  $stmt = $dbh->prepare($sql);  $stmt->execute(array(':login'=>'kevin2',':password'=>''));  
// echo $dbh->lastinsertid();  
// /*修改*/
// $sql = "UPDATE `user` SET `password`=:password WHERE `user_id`=:userId";  
// $stmt = $dbh->prepare($sql);  
// $stmt->execute(array(':userId'=>'7', ':password'=>'4607e782c4d86fd5364d7e4508bb10d9'));  
// echo $stmt->rowCount(); 
// 删除
// $sql = "DELETE FROM `user` WHERE `login` LIKE 'kevin_'"; //kevin%  
// $stmt = $dbh->prepare($sql);  
// $stmt->execute();  
// echo $stmt->rowCount();  
// /*查询*/
// $login = 'kevin%';  
// $sql = "SELECT * FROM `user` WHERE `login` LIKE :login";  
// $stmt = $dbh->prepare($sql);  
// $stmt->execute(array(':login'=>$login));  
// while($row = $stmt->fetch(PDO::FETCH_ASSOC)){     
//  print_r($row);  
// }  
// print_r( $stmt->fetchAll(PDO::FETCH_ASSOC)); 








      // $pdo = new PDO('localhost', 'root', 'password');
      // $pdo->setAttribute(PDO::ATTR_PERSISTENT, true); // 设置数据库连接为持久连接
      // $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // 设置抛出错误
      // $pdo->setAttribute(PDO::ATTR_ORACLE_NULLS, true); // 设置当字符串为空转换为 SQL 的 NULL
      // $pdo->query('SET NAMES utf8'); // 设置数据库编码
    } catch (PDOException $e) {
      exit('数据库连接错误，错误信息：'. $e->getMessage());
    }
    return $pdo;
  }
}
?>